#include "ohos_data_channel.h"

namespace ohoswebrtc {

OHOSDataChannelDelegateAdapter::~OHOSDataChannelDelegateAdapter() {
  
}

void OHOSDataChannelDelegateAdapter::RegisterObserver(OHOSDataChannelObserver* observer) {
  data_channel_observer_ = observer;
}

void OHOSDataChannelDelegateAdapter::UnRegisterObserver() {

}

void OHOSDataChannelDelegateAdapter::OnStateChange() {
  RTC_DCHECK(data_channel_observer_ != nullptr);
  if (data_channel_observer_ != nullptr) {
    data_channel_observer_->OnStateChange();
  }
}

void OHOSDataChannelDelegateAdapter::OnMessage(const webrtc::DataBuffer& buffer) {
  RTC_DCHECK(data_channel_observer_ != nullptr);
  if (data_channel_observer_ != nullptr) {
    data_channel_observer_->OnMessage(buffer);
  }
}

void OHOSDataChannelDelegateAdapter::OnBufferedAmountChange(uint64_t sent_data_size) {
  RTC_DCHECK(data_channel_observer_ != nullptr);
  if (data_channel_observer_ != nullptr) {
    data_channel_observer_->OnBufferedAmountChange(sent_data_size);
  }
}

OHOSDataChannel::OHOSDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
    : data_channel_(data_channel) {
  adapter_observer_.reset(new OHOSDataChannelDelegateAdapter());
  data_channel->RegisterObserver(adapter_observer_.get());
  label_ = data_channel_->label();
}

OHOSDataChannel::~OHOSDataChannel() {
  data_channel_->UnregisterObserver();
}

void OHOSDataChannel::Close() {
  data_channel_->UnregisterObserver();
  data_channel_->Close();
}

std::string OHOSDataChannel::label() const { 
  return label_; 
}

bool OHOSDataChannel::reliable() const {
  return data_channel_->reliable();
}

bool OHOSDataChannel::ordered() const {
  return data_channel_->ordered();
}

uint16_t OHOSDataChannel::maxRetransmitTime() const {
  return data_channel_->maxRetransmitTime();
}

uint16_t OHOSDataChannel::maxRetransmits() const {
  return data_channel_->maxRetransmits();
}

absl::optional<int> OHOSDataChannel::maxRetransmitsOpt() const {
  return data_channel_->maxRetransmitsOpt();
}

absl::optional<int> OHOSDataChannel::maxPacketLifeTime() const {
  return data_channel_->maxPacketLifeTime();
}

std::string OHOSDataChannel::protocol() const { 
  return data_channel_->protocol(); 
}

bool OHOSDataChannel::negotiated() const {
  return data_channel_->negotiated();
}

int OHOSDataChannel::id() const { 
  return data_channel_->id(); 
}

uint64_t OHOSDataChannel::buffered_amount() const {
  return data_channel_->buffered_amount();
}

void OHOSDataChannel::Send(const webrtc::DataBuffer& buffer) {
  data_channel_->Send(buffer);
}

void OHOSDataChannel::Send(const uint8_t* data, uint32_t size, bool binary) {
  rtc::CopyOnWriteBuffer copyOnWriteBuffer(data, size);
  webrtc::DataBuffer data_buffer(copyOnWriteBuffer, binary);
  data_channel_->Send(data_buffer);
}

void OHOSDataChannel::SendAsync(webrtc::DataBuffer buffer,
                         absl::AnyInvocable<void(webrtc::RTCError) &&> on_complete) {
  data_channel_->SendAsync(buffer, std::move(on_complete));
}  

webrtc::DataChannelInterface::DataState OHOSDataChannel::state() const { 
  return data_channel_->state(); 
}

uint32_t OHOSDataChannel::messages_sent() const {
  return data_channel_->messages_sent(); 
}

uint64_t OHOSDataChannel::bytes_sent() const {
  return data_channel_->bytes_sent(); 
}

uint32_t OHOSDataChannel::messages_received() const {
  return data_channel_->messages_received(); 
}

uint64_t OHOSDataChannel::bytes_received() const {
  return data_channel_->bytes_received(); 
}

void OHOSDataChannel::RegisterObserver(OHOSDataChannelObserver* observer) {
  if (observer != nullptr && adapter_observer_ != nullptr) {
    adapter_observer_->RegisterObserver(observer);
  }
}

void OHOSDataChannel::UnRegisterObserver() {
  if (adapter_observer_ != nullptr) {
    adapter_observer_->UnRegisterObserver();
  }
}
   
} 